package com.buddies.lottery.ui.register;

import com.buddies.lottery.App;
import com.buddies.lottery.base.BasePresenter;
import com.buddies.lottery.common.ConfigManager;
import com.buddies.lottery.data.BaseObserver;
import com.buddies.lottery.data.DataManager;
import com.buddies.lottery.data.resp.LoginResponse;

/**
 * Created by xuliang on 2018/2/28.
 */

public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {

    public RegisterPresenter(RegisterContract.View view) {
        super(view);
    }

    @Override
    public void postRegister(String loginId, String password) {
        addSubscription(DataManager.getInstance().getUserRepo().postRegister(loginId, password), new BaseObserver<LoginResponse>() {
            @Override
            public void onNext(LoginResponse response) {
                ConfigManager.getInstance().setUser(response.user);
                ConfigManager.getInstance().setToken(response.token);
                App.getInstance().initRetrofit();
                mView.registerSuccess(response.user);
            }

            @Override
            public void onError(Throwable e) {
               mView.onError(e);
            }
        });
    }

}
